﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_Media_File_Info : System.Web.UI.UserControl
{
    TblMediaManager _TblMediaManager = new TblMediaManager();
    public int MeID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { MeID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { MeID = 0; }

            loadCateParent(ddlCateParent,"category");
            loadCateParent(ddlNhomFile, "Typecategory");

            if (MeID == 0) //Add cate
            {
                lblTitle.Text = "Thêm file";
                Toolbox1.ShowDel = false;
            }
            else //sua cate
            {
                lblTitle.Text = "Sửa file: " + MeID;

                BindData(MeID);
            }
        }
    }

    protected void BindData(int ID)
    {
        TblMediaEntity _TblMediaEntity = _TblMediaManager.SelectOne(ID);
        if (_TblMediaEntity == null) { lblthongbao.Text = "<img src='../../Images/validatebox_warning.png' alt='Cảnh báo' width='16' height='16' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        hidID.Value = ID.ToString();
        txtID.Text = ID.ToString();
        txtFileName.Text =Server.HtmlDecode(_TblMediaEntity.TenMedia);
        txtMota.Text = Server.HtmlDecode(_TblMediaEntity.Description);
        ddlCateParent.SelectedValue = _TblMediaEntity.CateMediaId.ToString();
        ddlNhomFile.SelectedValue = _TblMediaEntity.NhomFileId.ToString();
        FileUpload1.FilePath = _TblMediaEntity.Link;
        chkStatus.Checked = Convert.ToBoolean(_TblMediaEntity.TrangThai);
    }

    private void loadCateParent(DropDownList _dll, string _type)
    {
        DataTable _dtDM = new DataTable();
        try
        {
            TblMediaCategoryEntity _TblMediaCategoryEntity = new TblMediaCategoryEntity();
            _TblMediaCategoryEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
            _TblMediaCategoryEntity.Type = _type;
            _dtDM = new TblMediaCategoryManager().be_SelectToDropdownlist(_TblMediaCategoryEntity);
        }
        catch { _dtDM = null; }

        if (_dtDM != null || _dtDM.Rows.Count > 0)
            HNG.LIB.BIND_SOURCE.Bind_DropDownListV3(_dll, _dtDM, TblDanhMucSanPhamFields.TenDanhMuc.Name, TblDanhMucSanPhamFields.Id.Name, TblDanhMucSanPhamFields.DanhMucCha.Name);
    }  

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblMediaEntity _TblMediaEntity = _TblMediaManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblMediaEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblMediaEntity.Link))
                {
                    string ImageDefault = Server.MapPath(_TblMediaEntity.Link);
                    try { File.Delete(ImageDefault); }
                    catch { }
                }
            }
            _TblMediaManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/FileEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        if (txtFileName.Text == "") { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Nhập tên danh mục !</span>"; txtFileName.Focus(); return; }
        TblMediaEntity _TblMediaEntity = new TblMediaEntity();
        if (!string.IsNullOrEmpty(hidID.Value)) _TblMediaEntity.Id = Convert.ToInt32(hidID.Value);//update
        _TblMediaEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblMediaEntity.TenMedia = Server.HtmlEncode(txtFileName.Text);
        _TblMediaEntity.CateMediaId = Convert.ToInt32(ddlCateParent.SelectedValue);
        _TblMediaEntity.CategoryName = ddlCateParent.SelectedItem.Text;
        _TblMediaEntity.NhomFileId = Convert.ToInt32(ddlNhomFile.SelectedValue);
        _TblMediaEntity.TenNhomFile = ddlNhomFile.SelectedItem.Text;
        _TblMediaEntity.Description = Server.HtmlEncode(txtMota.Text);
        _TblMediaEntity.TrangThai = (chkStatus.Checked == true) ? true : false;
        _TblMediaEntity.NgayDang = DateTime.Now;
        _TblMediaEntity.Type = "media";
        if (FileUpload1.FilePath != "")
        {
            if (FileUpload1.FilePath != _TblMediaEntity.Link)
            {
                string fileExtent = Path.GetExtension(Server.MapPath(FileUpload1.FilePath));
                _TblMediaEntity.DinhDang = fileExtent.ToUpper().Substring(1);

                string ImageDefault = Server.MapPath(_TblMediaEntity.Link);
                if (File.Exists(ImageDefault)) File.Delete(ImageDefault);
                _TblMediaEntity.Link = FileUpload1.FilePath;
            }
        }

        if (!string.IsNullOrEmpty(hidID.Value))
            _TblMediaManager.Update(_TblMediaEntity);//update
        else
        {
            _TblMediaManager.Insert(_TblMediaEntity);//Insert
            hidID.Value = _TblMediaEntity.Id.ToString();
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
}